<?php

define("NFO2_MODULES", NFO2_DB_PREFIX."modules");

	class module_factory extends service {
		
		public function produce($module_name){
			$path_root = context::get("path")->get_root();
			
			$path = $path_root._DRS."modules"._DRS.$module_name._DRS."{$module_name}.module.php";
			if ($this->valid_module($module_name)){
				include_once($path);
				
				$__module_name = "module_".$module_name;	//add prefix
				$module = new $__module_name;
				
				$module->set_name($module_name);
				
				return $module;
				
			}else{
				context::get("io")->debug("unable to locate '{$path}'",IO_DEBUG_ERROR);
				#throw new Core_Exception("Failed to load module '{$module_name}'.", EX_NONCRIT);
				
				return null;
			}
		}
		
		public function valid_module($module_name){
			
			$path_root = context::get("path")->get_root();
			$path = $path_root._DRS."modules"._DRS.$module_name._DRS."{$module_name}.module.php";
			
			if (!is_file($path)){
				return false;
			}

			if (!context::get("permissions")->allowed_module($module_name)){
				return false;
			}
			
			return true;
		}

	}
?>
